﻿using Microsoft.Office.Interop.Outlook;
using Outlook.WorkItems.Extension.Controls;

namespace Outlook.WorkItems.Extension.Addin.Actions
{
    public class InsertReportAction : IBowieAction
    {
        private readonly Attachments _attachments;

        public InsertReportAction(Attachments attachments)
        {
            _attachments = attachments;
        }

        public void Do()
        {
            using (var frm = new TeamReportViewerForm())
            {
                if (frm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    return;

                _attachments.Add(frm.FilePath, OlAttachmentType.olByValue);
            }
        }
    }


    public abstract class OutlookItem
    {
        public abstract Attachments Attachments { get; }
        public abstract string Description { get; set; }
        public abstract string Subject { get; set; }
        public abstract ItemProperties ItemProperties { get; }
        public abstract void SaveAs(string path, OlSaveAsType format);
        public abstract void Save();
    }

    public class OutlookMailItem : OutlookItem
    {
        private readonly MailItem _mailItem;
        public OutlookMailItem(MailItem item)
        {
            _mailItem = item;
        }

        public override Attachments Attachments
        {
            get
            {
                return _mailItem.Attachments;
            }
        }

        public override string Description
        {
            get { return _mailItem.HTMLBody; }
            set { _mailItem.HTMLBody = value; }
        }

        public override string Subject
        {
            get { return _mailItem.Subject; }
            set { _mailItem.Subject = value; }
        }

        public override ItemProperties ItemProperties
        {
            get { return _mailItem.ItemProperties; }
        }

        public override void SaveAs(string path, OlSaveAsType format)
        {
            _mailItem.SaveAs(path, format);
        }

        public override void Save()
        {
            _mailItem.Save();
        }
    }

    public class OutlookAppointmentItem : OutlookItem
    {
        private readonly AppointmentItem _appointmentItem;
        public OutlookAppointmentItem(AppointmentItem item)
        {
            _appointmentItem = item;
        }

        public override Attachments Attachments
        {
            get
            {
                return _appointmentItem.Attachments;
            }
        }

        public override string Description
        {
            get { return _appointmentItem.Body; }
            set { _appointmentItem.Body = value; }
        }

        public override string Subject
        {
            get { return _appointmentItem.Subject; }
            set { _appointmentItem.Subject = value; }
        }

        public override ItemProperties ItemProperties
        {
            get { return _appointmentItem.ItemProperties; }
        }

        public override void SaveAs(string path, OlSaveAsType format)
        {
            _appointmentItem.SaveAs(path, format);
        }

        public override void Save()
        {
            _appointmentItem.Save();
        }
    }

    public class OutlookTaskItem : OutlookItem
    {
        private readonly TaskItem _item;

        public OutlookTaskItem(TaskItem taskItem)
        {
            _item = taskItem;
        }

        public override Attachments Attachments
        {
            get { return _item.Attachments; }
        }

        public override ItemProperties ItemProperties
        {
            get { return _item.ItemProperties; }
        }

        public override string Description
        {
            get
            {
                return _item.Body;
            }
            set
            {
                _item.Body += value;
            }
        }

        public override string Subject
        {
            get
            {
                return _item.Subject;
            }
            set
            {
                _item.Subject = value;
            }
        }

        public override void SaveAs(string path, OlSaveAsType format)
        {
            _item.SaveAs(path, format);
        }

        public override void Save()
        {
            _item.Save();
        }


    }

}
